app.controller('TemplateImportCtrl', function ($scope, $modalInstance, $modal, toaster, param, ajax, fileUpload, cos) {

	$scope.uploading = false;
	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}

	$scope.example = function () {
		window.open('download?fileUrl=/ecar-mp/upload/班次档案批量导入示例.xlsx');
	}

	$scope.filePath = null;

	$scope.upload = function () {
		$scope.uploading = true;
		var uploadUrl = cos.host() + cos.img() + "?attributeName=excel";
		if ($scope.myForm.$valid) {
			console.log($scope.fileToUpload );
			if ($scope.fileToUpload != undefined) {
				toaster.pop('warning', '提示', '正在上传中，请不要关闭此页面！');
				fileUpload.upload($scope.fileToUpload, uploadUrl, null,
					function (result) {
						if (result.code == 200) {
							$scope.filePath = result.filePath;
							toaster.pop('success', '完成', '上传文件成功，点击提交开始导入数据。');
						} else {
							toaster.pop('error', '错误', result.message);
						}
						$scope.uploading = false;
					}, function () {
						toaster.pop('error', '错误', '文件上传失败，连接错误！');
						$scope.uploading = false;
					}
				);
			} else {
				toaster.pop('warning', '提示', '请上传附件！');
				$scope.uploading = false;
				return;
			}
		}
	};

	$scope.ok = function () {
		if ($scope.filePath == null){
			 toaster.pop('warning', '提示', '请上传一个Excel文件！');
			 return;
		}
		$modalInstance.close($scope.filePath);
		
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};
});